package com.xl.studentpass.controller;

import com.xl.studentpass.entity.TaskInfoEntity;
import com.xl.studentpass.entity.po.OrderOverViewPo;
import com.xl.studentpass.entity.po.UserDetailPo;
import com.xl.studentpass.entity.vo.UserVo;
import com.xl.studentpass.service.UserService;
import com.xl.studentpass.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author: xl
 * @createTime: 2023/5/28 10:55:00
 * @program: student-pass
 * @e-mail: 2199396150@qq.com
 * @description:
 */
@RestController
@RequestMapping("/user")
@Api(tags = "个人中心相关接口")
public class UserController {
    @Resource
    UserService userService;

    @PostMapping("/alterUser")
    @ApiOperation("修改用户")
    public R alterUser(@RequestBody UserVo user) {
        if (ObjectUtils.isEmpty(user) || ObjectUtils.isEmpty(user.getPhone())) {
            return R.error("输入错误");
        }
        return userService.update(user);
    }

    @PostMapping("/getUserInfo")
    @ApiOperation("获取用户信息")
    public R getUserDetail() {
        UserDetailPo userDetail = userService.getUserDetail();
        if (ObjectUtils.isEmpty(userDetail)) {
            return R.error();
        }
        return R.ok().put("userInfo", userDetail);
    }

    /**
     * 根据完成度获取对应的任务信息
     */
    @GetMapping("/getTask")
    @ApiOperation("根据完成度获取对应的任务信息")
    public R getTaskByStatus(@RequestParam("userId") Integer userId, @RequestParam(value = "statusId", required = false) Integer statusId) {
        List<TaskInfoEntity> taskInfos = userService.getTaskByStatus(userId, statusId);
        return R.ok().put("taskInfos", taskInfos);
    }

    @GetMapping("/getOrder")
    @ApiOperation("根据完成度获取对应的订单信息")
    public R getOrderByStatus(@RequestParam("userId") Integer userId, @RequestParam(value = "statusId", required = false) Integer statusId) {
        List<OrderOverViewPo> overViewPos = userService.getOrderByStatus(userId, statusId);
        if (ObjectUtils.isEmpty(overViewPos) || overViewPos.size() <= 0) {
            return R.error();
        }
        return R.ok().put("orderInfos", overViewPos);
    }

    @GetMapping("/getPostingInfoByFavorite")
    @ApiOperation("获取收藏帖子")
    public R getFavoritesCount(@RequestParam("userId") Integer userId) {
        return userService.getPostingInfosByFavorite(userId);
    }

    @GetMapping("/getPostingInfoByLike")
    @ApiOperation("获取点赞帖子")
    public R getLikeCount(@RequestParam("userId") Integer userId) {
        return userService.getPostingInfosByLike(userId);
    }

    @GetMapping("/getPostingInfoByComment")
    @ApiOperation("获取评论赞帖子")
    public R getCommentCount(@RequestParam("userId") Integer userId) {
        return userService.getPostingInfosByComment(userId);
    }


    @PostMapping("/recharge")
    public R topUp(@RequestBody Map<String, String> requestData) {
        if (ObjectUtils.isEmpty(requestData)) {
            return R.error("充值失败");
        }
        String userId = requestData.get("userId");
        if (ObjectUtils.isEmpty(userId)) {
            return R.error("充值失败");
        }
        return R.ok();
    }
}
